<?php

class Router
{
	private $DB;
	private $main_group;
	
	public function __construct($main_group)
	{
		$this->DB = DB::getInstance();
		
		$this->main_group = $main_group;
	}
	
	public function setMainGroup($main_group)
	{
		$this->main_group = $main_group;
	}
	
	public function parseURI($uri)
	{
		$segments = explode('/', $uri);
		
		if (empty($segments))
		{
			return false;
		}
		
		$this->parseSegment(0, $segments, 0, array());
	}
	
	private function parseSegment($parent_node, $segments, $current_index, $collected_data)
	{
		echo 'New parse segment -- at index ' . $current_index;
		Util::print_r($segments);
		
		foreach ($this->DB->PDO->query("
			SELECT `node`, `parent`, `segment`, `query`, `group`
			FROM `routesegment`
			WHERE `parent` = '" . $parent_node . "'
			  AND `group` = '" . $this->main_group . "'
		") as $potential)
		{
			if (substr($potential['segment'], 0, 1) == '%' && substr($potential['segment'], -1) == '%')
			{
				echo 'generic!<br />';
			}
			else
			{
				if ($potential['segment'] == $segments[$current_index])
				{
					$this->parseSegment($potential['node'], $segments, $current_index + 1, $collected_data);
				}
			}
		}
		
		echo '<br />';
	}
}

?>